/**
 * 对话框代码
 *
 * @type {string}
 */
let dialog_html = `
    <div class="al-dialog-container">
        <div class="af-dialog-shade"></div>
        <div class="af-dialog-box">
            <div class="af-dialog-box-hd">
                <div class="af-hd-left">对话框标题</div>
                <div class="af-hd-right">
                    <span>x</span>
                </div>
            </div>
            <div class="af-dialog-box-bd">
                <div id="al-dialog-content"></div>
            </div>
            <div class="af-dialog-box-ft">
                <button class="af-dialog-btn close-dialog-btn"> 关 闭 </button>
            </div>
        </div>
    </div>
`;

/**
 * 获取对话框配置信息
 */
function getDialogConfig(){
    let dialogConfig = window.$docsify.dialogConfig || {};
    // 是否开启对话框, 默认不启动弹框
    if (dialogConfig.isEnable == undefined){
        dialogConfig.isEnable = false;
    }
    if (!dialogConfig.width){
        dialogConfig.width = '950px';
    }
    if (!dialogConfig.height){
        dialogConfig.height = '600px';
    }
    if (!dialogConfig.left) {
        dialogConfig.left = 'calc(50% - ' + (parseInt(dialogConfig.width) / 2) + 'px)';
    }
    if (!dialogConfig.top) {
        dialogConfig.top = 'calc(50% - ' + (parseInt(dialogConfig.height) / 2) + 'px)';
    }
    if (!dialogConfig.title) {
        dialogConfig.title = '<span style="color: red;">对话框标题未设置</span>';
    }
    if (!dialogConfig.content) {
        dialogConfig.content = `<p>这里是是测试数据这里是是测试数据这里是是测试数据这里是是测试数据这里是是测试数据</p>`;
    }
    if (dialogConfig.isShowFt == undefined) {
        dialogConfig.isShowFt = false;
    }
    if (dialogConfig.coverpageShow == undefined){
        dialogConfig.coverpageShow = false;
    }
    if (dialogConfig.pageShow == undefined){
        dialogConfig.pageShow = false;
    }
    return dialogConfig;
}

/**
 * 对话框初始化
 */
function initDialog(){
    // 配置加载
    let dialogConfig = this.getDialogConfig();
    // 是否启动对话框
    if (!dialogConfig.isEnable) {
        return;
    }
    // 添加对话框html代码
    let $body = jQuery("body");
    $body.append(dialog_html);
    // 读取元素信息
    let afDialogBoxElement = document.querySelector('.af-dialog-box');
    // 是否显示footer
    let showFt = dialogConfig.isShowFt;
    if (!showFt) {
        document.querySelector('.af-dialog-box-ft').style.display = 'none';
        document.querySelector('.af-dialog-box-bd').style.bottom = '0';
    }
    // 设置对话框宽高
    afDialogBoxElement.style.height = dialogConfig.height;
    afDialogBoxElement.style.width = dialogConfig.width;
    afDialogBoxElement.style.top = dialogConfig.top;
    afDialogBoxElement.style.left = dialogConfig.left;
    // 设置对话框标题和内容
    jQuery('.af-hd-left').html(dialogConfig.title);
    jQuery('#al-dialog-content').html(dialogConfig.content);
}

/**
 * 关闭对话框
 *
 * @param event
 */
function closeDialogFunc(event) {
    let afDialogShadeElement = document.querySelector('.af-dialog-shade');
    let afDialogBoxElement = document.querySelector('.af-dialog-box');
    afDialogBoxElement.style.display = 'none';
    afDialogShadeElement.style.display = 'none';
    // document.body.style.overflow = 'auto';
    // 是否展开过
    let $alDialogContainer = jQuery(".al-dialog-container");
    $alDialogContainer.data("DIALOG_IS_SHOW", "true");
};

/**
 * 打开对话框
 *
 * @param event
 */
function openDialogFunc(event) {
    let afDialogShadeElement = document.querySelector('.af-dialog-shade');
    let afDialogBoxElement = document.querySelector('.af-dialog-box');
    afDialogBoxElement.style.display = '';
    afDialogShadeElement.style.display = '';
    // document.body.style.overflow = 'hidden';
};

/**
 * 初始化配置
 */
function initDialogEvent(){
    // 移动事件
    let afDialogBoxElement = document.querySelector('.af-dialog-box');
    afDialogBoxElement.addEventListener('mousedown', function(event) {
        // 计算鼠标在登录框中的坐标
        let x = event.pageX - afDialogBoxElement.offsetLeft;
        let y = event.pageY - afDialogBoxElement.offsetTop;
        // 给页面添加鼠标移动事件
        document.addEventListener('mousemove', dialogBoxMove);
        // 移动事件详情
        function dialogBoxMove(event) {
            // 计算移动后的位置
            let left = event.pageX - x;
            let top = event.pageY - y;
            // 获取浏览器视图的宽度和高度
            let browserWidth = window.innerWidth;
            let browserHeight = window.innerHeight;
            // 获取登录框的宽度和高度
            let afDialogBoxWidth = afDialogBoxElement.offsetWidth;
            let afDialogBoxHeight = afDialogBoxElement.offsetHeight;
            // 计算右边界和下边界
            let rightBoundary = browserWidth - afDialogBoxWidth;
            let bottomBoundary = browserHeight - afDialogBoxHeight;
            // 调整位置，确保不超出右下边界
            left = Math.min(Math.max(left, 0), rightBoundary);
            top = Math.min(Math.max(top, 0), bottomBoundary);
            // 设置登录框的位置
            afDialogBoxElement.style.left = left + "px";
            afDialogBoxElement.style.top = top + "px";
        }
        // 鼠标松开后移除页面的鼠标移动事件
        document.addEventListener('mouseup', function() {
            document.removeEventListener('mousemove', dialogBoxMove);
        });
    });
    // 加载事件
    document.querySelector('.af-hd-right span').addEventListener('click', this.closeDialogFunc);
    document.querySelector(".close-dialog-btn").addEventListener('click', this.closeDialogFunc);
}

/**
 * 载入插件
 *
 * @type {*[]}
 */
window.$docsify.plugins = [].concat(function(hook, vm) {

    // 钩子函数：每次路由切换时数据全部加载完成后调用，没有参数。
    hook.doneEach(function() {
        let dialogConfig = this.getDialogConfig();
        if (!dialogConfig.isEnable) {
            return;
        }
        let $alDialogContainer = jQuery(".al-dialog-container");
        if ($alDialogContainer.length == 0) {
            this.initDialog();
        }
        $alDialogContainer = jQuery(".al-dialog-container");
        let file = vm.route.file;
        let path = vm.route.path;
        let isCoverpagePage = true;
        if (file !== 'README.md' && path !== '/') {
            isCoverpagePage = false;
        }
        let pageShow = dialogConfig.pageShow;
        if (pageShow){
            this.openDialogFunc();
        }
        let coverpageShow = dialogConfig.coverpageShow;
        if (isCoverpagePage && !coverpageShow){
            this.closeDialogFunc();
            return;
        }
        let dialogIsShow = $alDialogContainer.data("DIALOG_IS_SHOW");
        if (!isCoverpagePage && (!dialogIsShow || dialogIsShow != "true")) {
            // $alDialogContainer.data("DIALOG_IS_SHOW", "true");
            this.openDialogFunc();
        }
    });

    // 初始化并第一次加载完成数据后调用，只触发一次，没有参数。
    hook.mounted(function() {
        let $alDialogContainer = jQuery(".al-dialog-container");
        if ($alDialogContainer.length == 0) {
            this.initDialog();
        }
        this.initDialogEvent();
    });

}, window.$docsify.plugins);
